<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>还款申请</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
    <script type="text/javascript" src="//cdn.staticfile.org/webuploader/0.1.5/webuploader.min.js"
            charset="utf-8"></script>
    <script src="//cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
</head>
<body class="iframeBody">
<div class="Wrapper" style="width:70%;">
    <div>
        <form id="repay_form">
            <div class="fieldset1 tabel_col clearFix">
                <div class="caption">
                    <span class="title">转  来  款  申  请</span>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">申请人</div>
                    <div class="field">
                        ${currentUserName}
                    </div>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">申请时间</div>
                    <div class="field">
                        ${currentTime}
                    </div>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">转来款金额</div>
                    <div class="field">
                        <span class="requiredSymbol" id="totalAmount" name="totalAmount"> </span>
                    </div>
                </div>
                <div class="fieldBox col_100">
                    <div class="label">备注</div>
                    <div class="field">
                        <textarea name="outRem" id="outRem" rows="6" class="ui-textarea l-textarea" style="width:80%"
                                  value="" validate="{maxlength:200}"
                                  <c:if test="${action == '3'}">disabled</c:if>>${outRem}</textarea>
                    </div>
                </div>
            </div>
            <div class="sqPanel">
                <div class="sqPanel_head inL">
                    <span>资 金 记 录</span>
                </div>
                <!-- 列表显示块 -->
                <div id="ligergrid1" class="sq-grid"></div>
            </div>


            <div class="sqPanel">
                <div class="sqPanel_head inL">
                    <span>附 件 上 传</span>
                    <%--<span class="btns"><span class="sq-button sq-button-round" onclick="downloadCstFileByUrl();">打包下载</span></span>--%>
                </div>
                <div class="sqPanel_body">
                    <div id="transIncomeFileGrid" class="sq-grid"></div>
                </div>
            </div>

            <div class="btnsBox" style="text-align: center">
                <c:if test="${action == '1'}">
                    <span id="applyBtn" class="sq-button sq-button-round" onclick="applyTrans('add')">发起申请</span>
                    <span id="closeBtn" class="sq-button sq-button-round" onclick="Remove_navTab()">取消</span>
                </c:if>
                <c:if test="${action == '2'}">
                    <span id="saveBtn" class="sq-button sq-button-round" onclick="applyTrans('modify')">保存</span>
                </c:if>
            </div>

            <input name="intoIds" id="intoIds" type="hidden" value="${intoIds}">
            <input name="outApplyId" id="outApplyId" type="hidden" value="${outApplyId}">
        </form>
    </div>
</div>

<script type="text/javascript">
    var ligerGrid, transIncomeFileGrid, outApplyId = $("#outApplyId").val();
    var actionUrl = '${action}';
    $(function () {
        // 初始化资金列表
        initFundList();

        initFileList();
    });

    function initFundList() {
        ligerGrid = $("#ligergrid1").ligerGrid({
            method: 'get',
            url: '<%=path%>/capital/capFundPool/fundsToApplyTransIncomeList.do?intoIds=' + $("#intoIds").val(),
            columns: [
                {
                    display: '资金编号', name: 'code', width: 220, enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '<a style="cursor: pointer;" onclick="qyDecompose(' + rowdata.fundId + ');">' + rowdata.code + '</a>'
                    }
                },
                {
                    display: '可用金额(元)', name: 'availableAmount', width: 170, enabledSort: false,
                    render: function (rowdata, rowindex, value, column) {
                        return '&yen;' + FormatMoney(value / 100);
                    }
                },
                {
                    display: '操作', minWidth: 100, width: 100, align: 'center', enabledSort: false,
                    render: function (rowdata, index, value) {
                        var action = '';
                        if (actionUrl == '1' || actionUrl == '2') {
                            action += '<a class="sq-button-hasicon" href="javascript:deleteRow(' + index + ');"><i class="sqBtnIco_17"></i>移除</a>';
                        }
                        return action;
                    }
                }
            ],
            onAfterShowData: function (e) {
                var rows = e.rows;
                var i = 0;
                for (var index in rows) {
                    i += rows[index].availableAmount;
                }
                $("#totalAmount").text('￥' + FormatMoney(i / 100));
            },
            usePager: false,//是否有分页便签
            root: 'rows',
            record: 'total',
            hideLoadButton: false,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            isScroll: false,//是否有滚动条
            checkbox: false,//列表是否有选择框
            enabledEdit: false,//是否能编辑
            rownumbers: true//列表是否有行号
        });
    }

    // 转来款资料配置明细Grid
    function initFileList() {
        transIncomeFileGrid = $("#transIncomeFileGrid").ligerGrid({
            url: '<%=path%>/capital/capFundFile/initTransIncomeFileList.do?outApplyId=' + outApplyId,
            method: 'get',
            columns: [
                {display: '资料名称', name: 'datNm', align: 'center', minWidth: 100, enabledSort: false},
                {
                    display: '上传文件名称', name: 'fileNm', align: 'center', minWidth: 100, enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        if (value) {
                            var h;
                            h = '<a href="javascript:uploadFileByUrl(';
                            h += "'" + rowdata.filePath + "'";
                            h += ",'" + rowdata.fileNm + "'";
                            h += ')">' + rowdata.fileNm + '</a>'
                            return h;
                        }
                    }
                },
                {
                    display: '上传时间',
                    name: 'crtTm',
                    align: 'center',
                    minWidth: 100,
                    enabledSort: false,
                    render: renderDate
                },
                // {display: '状态', name: 'audStsCdNm', width: 100, enabledSort: false},
                {
                    display: '操作', name: '', minWidth: 100, align: 'center', enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        var h = '';
                        if (rowdata.fileId && rowdata.audStsCd != 1) {
                            if (actionUrl == '1' || actionUrl == '2') {
                                h += '<a class="sq-button-hasicon" style="cursor:pointer;" onclick="delFile(event,' + rowdata.fileId + ');"><i class="sqBtnIco_7"></i>删除</a>';
                            }
                        }
                        return h;
                    }
                },
                {
                    display: '上传', name: 'uploadNm', width: 80, align: 'center', enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        var h = '';
                        if (actionUrl == '1' || actionUrl == '2') {
                            h += '<span class="sq-button sq-button-round fileupload-button">上传<input type="file" id="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '" multiple name="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '"' +
                                ' class="fileupload-input" onchange="onChangefile_multi(event,\'' + rowdata.cfgCd + '\',\'' + rowdata.cfgDtlCd + '\',\'' + rowdata.orgCd + '\')"></span>';
                        }
                        return h;
                    }
                }
            ],
            root: 'rows',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            isScroll: false,
            rownumbers: true,  //显示序号
            checkbox: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            frozen: false,
            usePager: false
        });
    }

    //查看分解单
    function qyDecompose(id) {
        Add_navTab('toDecomDtlPage_' + id, "分解单查看", '<%=path%>/capital/capFund/toDecomDtlPage.do?fundId=' + id, null);
    }

    // 移除
    function deleteRow(rowid) {
        $.ligerDialog.confirm("确定要移除?", function (flag) {
            if (flag == false) return;
            ligerGrid.deleteRow(rowid);
            var curDataGrid = ligerGrid.getData(),
                totalAmt = 0, _ids = [];
            curDataGrid.forEach(function (data, i) {
                totalAmt += data.availableAmount;
                _ids.push(data.id);
            });
            $("#totalAmount").text('￥' + FormatMoney(totalAmt / 100));
            $("#intoIds").val(_ids.join(','));
        });
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }

    function onChangefile_multi(e, cfgCd, cfgDtlCd, orgCd) {
        var wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
        var filePath = "capFileInfo";

        $.ajaxFileUpload({
            url: '<%=path%>/system/sysulfilesrec/upload.do',
            secureuri: false,
            fileElementId: e.target.id,  //这里不在是以前的id了，要写成数组的形式！
            dataType: 'json',
            data: {filePath: filePath},
            success: function (msg) {
                if (msg.success == 1) {
                    // 记录数据，等用户提交申请的时候做文件关联
                    var uploadData = {
                        'fileParams': JSON.stringify(msg.data),
                        'cfgCd': cfgCd,
                        'cfgDtlCd': cfgDtlCd,
                        'orgCd': orgCd
                    };
                    uploadData["dataId"] = outApplyId;
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: uploadData,
                        url: '<%=path%>/capital/capFundFile/uploadCapTransFile.do',
                        success: function (msg) {
                            if (msg.success == 1) {
                                transIncomeFileGrid.reload();
                            } else {
                                $.ligerDialog.SQalert({content: "资料上传超时", SQ_type: "error"});
                            }
                            $.ligerDialog.hide();
                        }
                    });
                    $.ligerDialog.SQalert({content: "资料上传成功", SQ_type: "info"});
                    transIncomeFileGrid.reload();
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
                wait_dialog.close();
            }
        });
    }

    // 发起申请
    function applyTrans(actionOpt) {
        var noticeInfo = "确定要申请吗？";
        if (actionOpt == 'modify') {
            noticeInfo = "确定要保存修改吗？";
        }
        $.ligerDialog.confirm(noticeInfo, function (flag) {
            if (flag == false) return;
            var out_rem = $("#outRem").val();
            var intoIds = $("#intoIds").val();
            if (intoIds == '') {
                $.ligerDialog.SQalert({content: "待转来款资金不能为空！", SQ_type: "warning"});
                return;
            }
            var requestRrl = '<%=path%>/capital/capFundPool/commitApply.do';
            if (actionOpt == 'modify') {
                requestRrl = '<%=path%>/capital/capFundPool/modifyApply.do';
            }
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {'outRem': out_rem, 'intoIds': intoIds, 'outApplyId': outApplyId},
                url: requestRrl,
                success: function (msg) {
                    if (msg.success == 1) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});

                        //Remove_navTab();
                        if (actionOpt == 'add') {
                            var TAB = top.mainApp.parentTab;
                            var currentTabid = TAB.getSelectedTabItemID();
                            var currFrameElement = top.$("#" + currentTabid).get(0);
                            var callback = currFrameElement.W2W.callback;
                            callback();
                            top.tab.removeTabItem(currentTabid);
                        }
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                    $.ligerDialog.hide();
                }
            });
        });
    }

    function uploadFileByUrl(_url, _name) {
        if (SQ.regs.img.test(_name)) {
            console.log("_url=" + _url + ",,_name=" + _name);
            //SQ.imgPreview(_url);
            //SQ.imgPreview(_url, true);
            window.__ImgSrc = _url;
            window.__ImgTit = _name;
            window.open('<%=path%>/capital/capFundFile/imgWinOpen.do', '', 'location=yes, width=800, height=600');
        } else {
            window.open(_url);
        }
    }

    //删除文件
    function delFile(event, _fileId) {
        top.$.ligerDialog.confirm("确认删除吗?", function (flag) {
            if (!flag) {
                return;
            } else {
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: {'fileId': _fileId},
                    url: '<%=path%>/capital/capFundFile/removeFile.do',
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({
                            content: LogUtils.LOG_CMM_DATA_LOADING,
                            SQ_type: "waitting"
                        });
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            event.stopPropagation();
                            var gridID = $(event.target).parents('.sq-grid').attr('ligeruiid');
                            liger.get(gridID).reload();
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            }
        });
    }
</script>
</body>
</html>